﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ex3.Logic;
using FacebookWrapper.ObjectModel;
using Ex3.Repository;

namespace Ex3
{
    public delegate void CreatedCircleDelegate(Circle i_Circle);

    public partial class ComposerForm : Form
    {
        internal User m_LoggedInUser { get; set; }

        private Builder m_BuilderOfCircles { get; set; }

        private Circle m_NewCircle { get; set; }

        private CreatedCircleDelegate m_CreatedCircleDelegate;

        public ComposerForm(User i_LoggedInUser)
        {
            this.m_LoggedInUser = i_LoggedInUser;
            this.InitializeComponent();
            this.m_BuilderOfCircles = new BuilderOfCircles();
            this.m_NewCircle = new Circle();
        }

        /// <summary>
        /// search inside the list of friends
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBoxFindFriendInList_TextChanged(object sender, EventArgs e)
        {
            this.checkedListBoxFriendList.ClearSelected();
            int foundIdx = this.checkedListBoxFriendList.FindString(this.textBoxFindFriendInList.Text);
            if (foundIdx > -1)
            {
                // if an exact match, select the match
                if (this.textBoxFindFriendInList.Text.ToLower() ==
                    this.checkedListBoxFriendList.Items[foundIdx].ToString().ToLower())
                {
                    this.checkedListBoxFriendList.SelectedIndex = foundIdx;
                }

                // scroll to the closest match:
                this.checkedListBoxFriendList.TopIndex = foundIdx;
            }
        }

        private void initializeCheckedListBox()
        {
            this.checkedListBoxFriendList.DisplayMember = "Name";
            foreach (User friend in this.m_LoggedInUser.Friends)
            {
                this.checkedListBoxFriendList.Items.Add(friend);
            }
        }

        private void ComposerForm_Shown(object sender, EventArgs e)
        {
            this.initializeCheckedListBox();
        }

        private void buttonCreate_Click(object sender, EventArgs e)
        {
            this.constructCircle();
        }

        private void constructCircle()
        {
            List<User> usersList = new List<User>();

            if (this.textBoxCircleType.Text == string.Empty)
            {
                MessageBox.Show("You need to insert name for the circle");
            }
            else
            {
                if (this.listBoxFriendsToAddToTheNewCircle.Items.Count == 0)
                {
                    this.m_BuilderOfCircles.BuildCircleName(this.textBoxCircleType.Text);
                    this.m_NewCircle = this.m_BuilderOfCircles.GetNewCircle();
                }
                else
                {
                    foreach (User user in this.listBoxFriendsToAddToTheNewCircle.Items)
                    {
                        usersList.Add(user);
                    }

                    this.m_BuilderOfCircles.BuildCircleName(this.textBoxCircleType.Text);
                    this.m_BuilderOfCircles.BuildFriends(usersList);
                    this.m_NewCircle = this.m_BuilderOfCircles.GetNewCircle();
                }

                this.notifyCreateCircleObservers();
                this.Close();
            }
        }

        private void checkedListBoxFriendList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.listBoxFriendsToAddToTheNewCircle.Items.Clear();

            this.listBoxFriendsToAddToTheNewCircle.DisplayMember = "Name";

            foreach (User friend in this.checkedListBoxFriendList.CheckedItems)
            {
                this.listBoxFriendsToAddToTheNewCircle.Items.Add(friend);
            }
        }

        public void AttachObserver(CreatedCircleDelegate i_ObserverDelegate)
        {
            this.m_CreatedCircleDelegate += i_ObserverDelegate;
        }

        public void DetachObserver(CreatedCircleDelegate i_ObserverDelegate)
        {
            this.m_CreatedCircleDelegate -= i_ObserverDelegate;
        }

        private void notifyCreateCircleObservers()
        {
            if (this.m_CreatedCircleDelegate != null)
            {
                foreach (CreatedCircleDelegate observerDelegate in this.m_CreatedCircleDelegate.GetInvocationList())
                {
                    this.m_CreatedCircleDelegate.Invoke(this.m_NewCircle);
                }
            }
        }
    }
}
